﻿/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shuriken.Base;

namespace Shuriken.Core
{
    public delegate void ApplicationCommand(string param);

    public class ApplicationService : IApplicationService
    {
        Dictionary<string, ApplicationCommand> commands;

        public ApplicationService()
        {
            commands = new Dictionary<string, ApplicationCommand>();

            commands.Add("Application.About", delegate(string param) { new Shuriken.frmAbout().ShowDialog(); });
            commands.Add("Application.Settings", delegate(string param) { new Shuriken.frmSettings().ShowDialog(); });
            commands.Add("Application.Exit", delegate(string param) { System.Windows.Forms.Application.Exit();  });
            commands.Add("Application.DisplayNotification", delegate(string param) { UI.UIManager.GetDefaultInstance().ShowNotificationAction(param); });
            commands.Add("Application.RevealExecutable", new ApplicationCommand(ApplicationRevealExecutable));
            commands.Add("Application.OpenLogFile", new ApplicationCommand(ApplicationOpenLogFile));
        }

        #region IApplicationService Members

        public bool ExecuteCommand(string subject, string action)
        {
            return ExecuteCommand(subject, action, string.Empty); 
        }

        public bool ExecuteCommand(string subject, string action, string attribute)
        {
            string command = subject + "." + action;

            if (commands.ContainsKey(command))
            {
                ApplicationCommand appCommand = commands[command];

                if (appCommand != null)
                {
                    appCommand.Invoke(attribute);
                    return true;
                }
            }

            return false; 
        }

        #endregion

        public void ApplicationRevealExecutable(string param)
        {
            string entry_file = System.Reflection.Assembly.GetEntryAssembly().Location;
            System.Diagnostics.Process.Start("explorer.exe", "/n,/select," + entry_file);
        }

        public void ApplicationOpenLogFile(string param)
        {
            System.Diagnostics.Process.Start("explorer.exe", " " + ApplicationEnvironment.LogFilePath);
        }
    }
}
